#!/usr/bin/env bash
# create_nginx_conf.sh
# 功能：自动生成 Nginx 反向代理配置文件和相关的帮助/错误页面
# 作者：Mobufan
# 版本：1.2  统一颜色风格

set -e

# ============================== 颜色变量 ==============================
gl_hui='\e[37m'
gl_hong='\033[31m'
gl_lv='\033[32m'
gl_huang='\033[33m'
gl_lan='\033[34m'
gl_zi='\033[35m'
gl_bufan='\033[96m'
gl_bai='\033[0m'
# ====================================================================


# 安全的输入函数，支持退格键
safe_input() {
    local prompt="$1"
    local default_value="$2"
    local input_value=""

    while true; do
        read -rep "$prompt" input_value
        # 如果用户只按回车，使用默认值
        if [[ -z "$input_value" && -n "$default_value" ]]; then
            echo "$default_value"
            return 0
        elif [[ -n "$input_value" ]]; then
            echo "$input_value"
            return 0
        else
            echo -e "${gl_huang}输入不能为空，请重新输入${gl_bai}"
        fi
    done
}

# 智能端口输入：优先匹配预设，回车用预设，无预设则 8888
smart_port_input(){
    local svc="$1"
    local default_port="${PRESET_PORTS[$svc]:-8888}"
    local input_port

    read -rep "请输入后端端口（默认端口 ${default_port}）: " input_port
    [[ -z "$input_port" ]] && input_port="$default_port"
    echo "$input_port"
}

# URL输入函数（允许为空）
safe_url_input() {
    local prompt="$1"
    local input_value=""

    read -rep "$prompt" input_value
    echo "$input_value"
}

# ========== 预设端口表 ==========
declare -A PRESET_PORTS=(
    [transmission]=9091
    [halo]=8090
    [emby]=8896
    [fntv-302]=8095
    [md]=9900
    [xiaomusic]=8393
    [metube]=8081
    [random-pic-api]=8588
    [easynode]=8082
    [allinssl]=7979
    [ittools]=8088
    [sun-panel]=3002
    [sun-panel-helper]=33002
    [openlist]=5244
    [taosync]=8023
    [xunlei]=2345
    [qbittorrent]=8080
    [musicn]=7478
    [dufs]=5000
    [nastools]=3000
    [jackett]=9117
    [aipan]=5055
    [libretv]=8899
    [moontv]=3133
    [cloud-saver]=8008
    [pansou]=8110
    [panhub]=3020
    [mind-map]=8256
    [easyvoice]=3780
    [dpanel]=8807
    [kspeeder]=5003
    [speedtest]=7878
    [navidrome]=4533
    [uptime-kuma]=3001
    [chinesesubfinder]=19035
    [nginx-file]=18080
)

# 显示欢迎信息
clear
echo ""
echo -e "${gl_zi}>>> Nginx 配置生成脚本${gl_bai}"
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_lan}作者: Mobufan${gl_bai}"
echo -e "${gl_lv}功能: 自动创建Nginx反向代理配置和相关页面${gl_bai}"
echo -e "${gl_bufan}------------------------${gl_bai}"
echo ""
echo -e "${gl_lv}>>>>>>>>>>>>>>>>${gl_bai} 脚本开始 ${gl_lv}<<<<<<<<<<<<<<<<${gl_bai}"
echo ""
# ========== 1. 服务名称配置 ==========
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_huang}步骤 1/9: 服务名称配置${gl_bai}"
SVC_NAME=$(safe_input "请输入服务名称（默认名称 aaaa）: " "aaaa")
echo -e "${gl_lv}✓ 服务名称: $SVC_NAME${gl_bai}"
echo -e "${gl_lv}✓ 说明页面 - 路径和名称：/etc/nginx/html/help/${SVC_NAME}_help${gl_bai}"
echo -e "${gl_lv}✓ 错误页面 - 路径和名称：/etc/nginx/html/error/${SVC_NAME}_error${gl_bai}"
echo -e "${gl_lv}✓ 配置文件 - 路径和名称：/etc/nginx/conf.d/${SVC_NAME}.conf${gl_bai}"
echo ""

# ========== 2. 子域名前缀 ==========
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_huang}步骤 2/9: 域名配置${gl_bai}"
SUB_DOM=$(safe_input "请输入域名前缀（默认前缀 ${SVC_NAME}）：" "$SVC_NAME")
DOMAIN="${SUB_DOM}.mobufan.eu.org"
echo -e "${gl_lv}✓ ${gl_bai}域名: ${gl_lv}$DOMAIN${gl_bai}"
echo ""

# ========== 3. 后端服务配置 ==========
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_huang}步骤 3/9: 后端服务配置${gl_bai}"
BACKEND_IP=$(safe_input "请输入后端 IP（默认地址 10.10.10.251）: " "10.10.10.251")
echo -e "${gl_lv}✓ ${gl_bai}后端 IP: ${gl_lv}$BACKEND_IP${gl_bai}"
echo ""

# ========== 4. 后端端口配置 ==========
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_huang}步骤 4/9: 后端端口配置${gl_bai}"
BACKEND_PORT=$(smart_port_input "$SVC_NAME")
echo -e "${gl_lv}✓ ${gl_bai}后端端口: ${gl_lv}$BACKEND_PORT${gl_bai}"
echo ""

# ========== 5. 生成 Nginx 配置文件 ==========
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_huang}步骤 5/9: 生成 Nginx 配置文件${gl_bai}"
CONF_FILE="/etc/nginx/conf.d/${SVC_NAME}.conf"

cat > "${CONF_FILE}" <<EOF
server {
    listen 666 ssl;
    listen [::]:666 ssl;
    http2 on;

    server_name ${DOMAIN};
    ssl_certificate /etc/nginx/keyfile/mobufan.eu.org.pem;
    ssl_certificate_key /etc/nginx/keyfile/mobufan.eu.org.key;
    client_max_body_size 1024G;

    # 定义日志文件路径和格式
#    error_log /etc/nginx/log/mobufan.eu.org/${SVC_NAME}_error.log;               # 错误日志
#    access_log /etc/nginx/log/mobufan.eu.org/${SVC_NAME}_access.log mobufan_log; # 访问日志

    # 错误页面
    error_page 404 500 502 503 504 /index.html;
    location = /index.html {
        root   /etc/nginx/html/error/${SVC_NAME}_error;
        sub_filter_once on;
        sub_filter '<head>' '<head><link rel="icon" href="/error-favicon.ico" type="image/x-icon">';
    }
    location = /error-favicon.ico {
        alias /etc/nginx/html/error/${SVC_NAME}_error/favicon.ico;
        access_log off;
    }

    location / {
        proxy_pass http://${BACKEND_IP}:${BACKEND_PORT};
        proxy_set_header Host \$http_host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
        proxy_set_header X-Forwarded-Host \$host;
        proxy_set_header X-Forwarded-Port \$server_port;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection \$http_connection;
        proxy_set_header Range \$http_range;
        proxy_set_header If-Range \$http_if_range;
        proxy_connect_timeout 30s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
        proxy_buffering on;
        proxy_buffer_size 16k;
        proxy_buffers 8 32k;
        proxy_busy_buffers_size 64k;
        proxy_max_temp_file_size 1024m;
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_cache_bypass \$http_upgrade;
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options SAMEORIGIN;
        add_header X-XSS-Protection "1; mode=block";
        add_header Referrer-Policy "strict-origin-when-cross-origin";
        proxy_hide_header X-Powered-By;
        proxy_hide_header Server;
    }

    # 说明页面
    location ^~ /h {
        alias /etc/nginx/html/help/${SVC_NAME}_help;
        index index.html;
        try_files \$uri \$uri/ =404;
        access_log off;
    }
}
EOF

echo -e "${gl_lv}✓ ${gl_bai}已生成配置文件：${gl_huang}${CONF_FILE}${gl_bai}"
echo ""

# ========== 6. 创建日志目录 ==========
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_huang}步骤 6/9: 创建日志目录${gl_bai}"
LOG_DIR="/etc/nginx/log/mobufan.eu.org"
echo -e "${gl_bufan}正在创建日志目录并授权 ...${gl_bai}"

sudo mkdir -p "$LOG_DIR" || { echo -e "${gl_hong}✗ 创建日志目录失败！${gl_bai}"; exit 1; }

NGX_USER=$(nginx -V 2>&1 | grep -oP 'user=\K\w+' || echo "nginx")
[[ -z "$NGX_USER" || "$NGX_USER" == "nginx" ]] && \
    NGX_USER=$(grep -m1 -E '^user[[:space:]]+' /etc/nginx/nginx.conf | awk '{print $2}' | tr -d ';')

sudo chown -R "$NGX_USER:$NGX_USER" "$LOG_DIR"
sudo chmod 755 "$LOG_DIR"

echo -e "${gl_lv}✓ 日志目录已创建/授权：$LOG_DIR （user: $NGX_USER）${gl_bai}"
echo ""

# ========== 7. 复制并替换说明/错误页面模板 ==========
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_huang}步骤 7/9: 生成帮助和错误页面${gl_bai}"
HELP_SRC="/etc/nginx/html/help/test-xxx_help"
HELP_DST="/etc/nginx/html/help/${SVC_NAME}_help"
ERROR_SRC="/etc/nginx/html/error/test-xxx_error"
ERROR_DST="/etc/nginx/html/error/${SVC_NAME}_error"

echo -e "${gl_bufan}正在复制模板文件...${gl_bai}"
sudo rsync -a --delete "$HELP_SRC"/ "$HELP_DST"/
sudo rsync -a --delete "$ERROR_SRC"/ "$ERROR_DST"/

echo -e "${gl_bufan}正在替换模板变量...${gl_bai}"
sudo sed -i "s/test-xxx/${SVC_NAME}/g" "$HELP_DST/index.html"
sudo sed -i "s/test-xxx/${SVC_NAME}/g" "$ERROR_DST/index.html"
sudo sed -i "s/test-yyy/${SUB_DOM}/g" "$ERROR_DST/index.html"
echo ""

# ========== 8. 修改帮助文档 URL ==========
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_huang}步骤 8/9: 修改帮助文档 URL（按回车跳过设置）${gl_bai}"
HEXO_URL=$(safe_url_input "请输入 Hexo 地址: ")
HUGO_URL=$(safe_url_input "请输入 Hugo 地址: ")
HALO_URL=$(safe_url_input "请输入 Halo 地址: ")

if [[ -n "$HEXO_URL" || -n "$HUGO_URL" || -n "$HALO_URL" ]]; then
    sudo sed -i \
      -e "s|hexourl|${HEXO_URL}|g" \
      -e "s|hugourl|${HUGO_URL}|g" \
      -e "s|halourl|${HALO_URL}|g" \
      "$HELP_DST/index.html"
    echo -e "${gl_lv}✓ URL 配置已更新${gl_bai}"
else
    echo -e "${gl_huang}⚠ 未设置URL，使用模板默认值${gl_bai}"
fi
echo ""

# ========== 9. 检查 Nginx 语法并重启服务 ==========
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_huang}步骤 9/9: 检查 Nginx 语法并重启服务${gl_bai}"
echo -e "${gl_huang}正在检查 Nginx 语法并重启服务 ...${gl_bai}"

if sudo nginx -t; then
    echo -e "${gl_lv}✓ 语法检测通过，正在重启 Nginx ...${gl_bai}"
    sudo systemctl restart nginx && \
        echo -e "${gl_lv}✓ Nginx 已重启，新配置生效！${gl_bai}" || \
        { echo -e "${gl_hong}✗ 重启失败，请手动排查！${gl_bai}"; exit 1; }
else
    echo -e "${gl_hong}✗ 语法有误，已中止重启，请修正后再试！${gl_bai}"
    exit 1
fi
echo ""

echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_lan}说明页面：$HELP_DST${gl_bai}"
echo -e "${gl_lan}错误页面：$ERROR_DST${gl_bai}"
echo -e "${gl_lan}配置文件：$CONF_FILE${gl_bai}"
echo -e "${gl_zi}访问地址：${gl_lv}https://$DOMAIN:666${gl_bai}"
echo -e "${gl_zi}帮助页面：${gl_lv}https://$DOMAIN:666/h${gl_bai}"
echo -e "${gl_bufan}------------------------${gl_bai}"
echo -e "${gl_bufan}重新加载配置：sudo nginx -t && sudo nginx -s reload${gl_bai}"
echo -e "${gl_bufan}重启   Nginx：sudo nginx -t && sudo systemctl restart nginx${gl_bai}"
echo -e "${gl_bufan}删除重新配置：rm -rf $HELP_DST $ERROR_DST $CONF_FILE${gl_bai}"
echo -e "${gl_bufan}------------------------${gl_bai}"